package com.yyfax.ph.home.getrecommendPermission;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.View;
import butterknife.BindView;
import butterknife.OnClick;
import com.yyfax.common.form.ClearEditText;
import com.yyfax.common.tools.DensityUtil;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.ph.R;
import com.yyfax.ph.base.BaseYYFaxActivity;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.base.dialog.DialogBase;
import com.yyfax.ph.home.recommendloan.RecommendLoanActivity;
import com.yyfax.ph.personal.authority.bankcard.view.activity.BindCardActivity;

/**
 * 获得推荐权限界面
 * Created by zhangxian on 2017/6/17.<br>
 * Copyright 2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class GetRecommendPermissionActivity extends BaseYYFaxActivity implements GetRecommendPermissionPresenter.View {
	private static final int LOADER_ID = 401;

	private static final String COMPANY = "company";
	@BindView(R.id.company_input_edt)
	ClearEditText mCompanyEdt;

	private GetRecommendPermissionPresenter mPresenter;

	public static void start(Activity aty, Intent intent) {
		aty.startActivity(intent);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return new PresenterLoader<>(this, new GetRecommendPermissionPresenter.Generator());
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		mPresenter = (GetRecommendPermissionPresenter) data;
		mPresenter.attach(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		getSupportLoaderManager().destroyLoader(LOADER_ID);
	}

	@Override
	protected void onStart() {
		super.onStart();
		getSupportLoaderManager().initLoader(LOADER_ID, null, this);
	}

	@Override
	public int getContentViewRes() {
		return R.layout.get_recommend_permission_activity;
	}

	@Override
	public void initContentView() {
		super.initContentView();
		setBtnRightVisible(false);
		setTitleLeftVisible(false);
		setTitleVisible(true);
		setTitle(R.string.get_recommend_permission);
		mCompanyEdt.setBackgroundColor(getResources().getColor(R.color.color_ffffff));
		mCompanyEdt.setPadding(DensityUtil.dip2px(this, 15), mCompanyEdt.getPaddingTop(), DensityUtil.dip2px(this, 15), mCompanyEdt.getPaddingBottom());
	}

	@Override
	public void onBackBtnPressed() {
		super.onBackBtnPressed();
		finish();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

	//邀请码/公司编号无效
	@Override
	public void invalidnumber() {
		showToast(R.string.invalid_invited_code);
	}

	//推荐权限激活,用户已实名
	@Override
	public void hasVerfied() {
		showToast(R.string.permission_activate_success);
		RecommendLoanActivity.start(this);
		finish();
	}

	//推荐权限激活,用户未实名
	@Override
	public void notVerfied() {
		showToast(R.string.permission_activate_success);
		Intent intent = new Intent(this, BindCardActivity.class);
		intent.putExtra(BindCardActivity.LAST_PAGE, BindCardActivity.FROM_HOME_FRAGMENT);
		BindCardActivity.start(this, intent);
		finish();
	}

	//绑定母公司申请
	@Override
	public void isCompanyCode(final String company) {
		dismissLoading();
		showTipsDialog(getString(R.string.bind_request_sent), new DialogBase.BottomOneClickListener() {
			@Override
			public void bottomOneClick(DialogBase dialog) {
				finish();
			}
		}, false);
	}

	@Override
	public void bindCompanyFailure(String tips) {
		dismissLoading();
		showToast(tips);
	}

	@OnClick(R.id.bind_company_tv)
	public void onClick(View view) {
		super.onClick(view);
		switch (view.getId()) {
			case R.id.bind_company_tv:
				String channel = mCompanyEdt.getText().toString();
				if (!TextUtils.isEmpty(channel)) {
					if (channel.length() > 10) {//todo 无用, xml 文件里面
						showToast(R.string.channel_number_digits_error);
					} else {
						YJPH.hideInputWindow(GetRecommendPermissionActivity.this);//隐藏键盘
						mPresenter.bindCompany(mCompanyEdt.getText().toString());
					}
				} else {
					showToast(R.string.please_input_invited_code_or_company_code);
				}
				break;
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(COMPANY, mCompanyEdt.getText().toString());
	}

	@Override
	public void restoreInstanceState(Bundle savedInstanceState) {
		super.restoreInstanceState(savedInstanceState);
		if (savedInstanceState != null) {
			mCompanyEdt.setText(savedInstanceState.getString(COMPANY));
		}
	}
}
